package com.demo.test;

import com.demo.bean.Emp;
import com.demo.bean.EmpVo;
import com.demo.bean.User;
import com.demo.mapper.EmpMapper;
import com.demo.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class test {
    private SqlSession sqlSession = null;
    @Before
    public void init() throws IOException {
        String path = "mybatis_config.xml";
        //获取 对该资源文件的输入流
        InputStream ins = Resources.getResourceAsStream(path);
        //通过sqlSessionFactoryBuilder加载配置文件 来创建会话工厂。
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(ins);
        sqlSession = sqlSessionFactory.openSession();
    }

    @Test
    public void select01() {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = userMapper.selectAll();
        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test
    public void select02() {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        Map<String,String> userMap = new HashMap<String,String>();
        userMap.put("userId","1");
        userMap.put("userName","zhangsan");
        User user = userMapper.selectUserByNameAndId(userMap);
        System.out.println(user);
    }

    @Test
    public void slectemp01(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> empVoList = empMapper.selectAll();
        for (Emp emp : empVoList) {
            System.out.println(emp);
        }
    }


    @After
    public void select03() {
        sqlSession.commit();
        sqlSession.close();
    }
}
